package org.fage.semples.web;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.fage.semples.dao.ExamScoreHiberanteDAO;
import org.fage.semples.dao.ExamTestHiberanteDAO;
import org.fage.semples.model.ExamScore;
import org.fage.semples.model.ExamScores;
import org.fage.semples.model.ExamTest;
import org.fage.semples.model.ExamUserSubjectScoreVO;
import org.fage.semples.model.ExamUserVO;
import org.fage.semples.model.manageGrades;
import org.fage.semples.service.ExamSubjectHiberanteService;
import org.fage.semples.service.ExamUserSubjectHiberanteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {
	
	@Autowired
	private ExamUserSubjectHiberanteService examUserSubjectHiberanteService;
	
	@Autowired
	private ExamSubjectHiberanteService examSubjectHiberanteService;
	
	@Autowired
	private ExamTestHiberanteDAO examTestHiberanteDAO;
	
	@Autowired
	private ExamScoreHiberanteDAO examScoreHiberanteDAO;
	
	//该学生的考试科目
	@RequestMapping("/studentExams")
	public String studentExamList(Model model,HttpSession session){
		ExamUserVO examUserVO = (ExamUserVO) session.getAttribute("examUser");
		
		List<ExamUserSubjectScoreVO> examUserSubjectScoreVO = examUserSubjectHiberanteService.getByUserIdExamUserSubjects(examUserVO.getUserId());
		model.addAttribute("studentExamList", examUserSubjectScoreVO);
		return "user/studentExam";
	}
	
	//学生进入考试
	@RequestMapping(value="/StudentStartTest")
	public String StudentStartTest(Model model,String subjectId,String userId) throws Exception{
		model.addAttribute("ExamTestList", examTestHiberanteDAO.findAllExamTestOrExamTestType(subjectId));
		model.addAttribute("exam", examSubjectHiberanteService.getExamSubjectById(subjectId));
		return "studentTest/startTests";
	}
	
	//计算学生考试成绩
	@RequestMapping(value="/StudentTestGrade")
	public String StudentTestGrade(Model model,HttpServletRequest request,HttpSession session) throws Exception{
		String subjectId = request.getParameter("subjectId");
		ExamUserVO examUserVO = (ExamUserVO) session.getAttribute("examUser");
		List<ExamTest> examTestList = examTestHiberanteDAO.findAllExamTestOrExamTestType(subjectId);
		int score = 0 ;
		for(ExamTest examTest:examTestList){
			String[] chooses = request.getParameterValues(""+examTest.getId());
			String sc = "";
			for(String choose:chooses){
				sc += choose + ",";
			}
			String isChooseOk = examTest.getAnswer()+",";
			if(sc.equals(isChooseOk)){
				score = score + examTest.getExamTestType().getTestTypeScore();
			}
		}
		//保存成绩
		ExamScore examScore = new ExamScore();
		examScore.setScore(score);
		examScore.setSubjectId(Integer.parseInt(subjectId));
		examScore.setUserId(examUserVO.getUserId());
		examScore.setExamTime(new Date());
		examScoreHiberanteDAO.addUserExamScore(examScore);
		model.addAttribute("subjectName",examSubjectHiberanteService.getExamSubjectById(subjectId).getName());
		model.addAttribute("score", score);
		model.addAttribute("time", examScore.getExamTime());
		
		return "studentTest/overTests";
	}
	
	//老师管理学生成绩
	@RequestMapping( value = "/manageGrade",method = RequestMethod.GET)
	public String manageGrade(Model model) throws Exception{
		
		model.addAttribute("examSubject", examSubjectHiberanteService.findAllExamSubject());
		model.addAttribute("studentScoreList", getManageGradesList());
		return "StudentGrade/manageGrade";
	}
	
	//教师选择查询条件,查询考试信息
	@RequestMapping(value="/FindManageGrade",method = RequestMethod.POST)
	public @ResponseBody List<manageGrades> FindManageGrade(String subjectId,String studentId,String studentName,String testTime) throws Exception{
		
		List<manageGrades> manageGradeList = getManageGradesList();
		if(!subjectId.equals("")){
			for(int i = 0; i < manageGradeList.size();i++){
				if(!subjectId.equals(manageGradeList.get(i).getSubjectId())){
					manageGradeList.remove(i);
					i--;
				}
			}
		}
		
		if(!studentId.equals("")){
			for(int i = 0; i < manageGradeList.size();i++){
				if(!studentId.equals(manageGradeList.get(i).getStudentId())){
					manageGradeList.remove(i);
					i--;
				}
			}
		}
		
		if(!studentName.equals("")){
			for(int i = 0; i < manageGradeList.size();i++){
				if(!studentName.equals(manageGradeList.get(i).getStudentName())){
					manageGradeList.remove(i);
					i--;
				}
			}
		}
		
		if(!testTime.equals("")){
			for(int i = 0; i < manageGradeList.size();i++){
				if(!testTime.equals(manageGradeList.get(i).getTestTime())){
					manageGradeList.remove(i);
					i--;
				}
			}
		}
		
		return manageGradeList;
	}
	
	public List<manageGrades> getManageGradesList() throws Exception{
		List<ExamScores> examScoresList = examScoreHiberanteDAO.findAllExamScores();
		List<manageGrades> manageGradeList = new ArrayList<manageGrades>();
		manageGrades mg = null;
		for(ExamScores examScores:examScoresList){
			mg = new manageGrades();
	        
			mg.setSubjectId(examScores.getExamSubjects().getSubId());
			mg.setStudentId(examScores.getUsers().getUserId());
			mg.setStudentName(examScores.getUsers().getUserName());
			mg.setExamSubject(examScores.getExamSubjects().getName());
			
			SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
	        SimpleDateFormat s2 = new SimpleDateFormat("yyyy-MM-dd");  
	        Date tempDate =null;  
	        String outTime = null;  
	        tempDate = s1.parse(examScores.getExamTime());  
	        outTime = s2.format(s2.parse(s1.format(tempDate))); 
			mg.setTestTime(outTime);
			
			mg.setExamScore(examScores.getScore());
			manageGradeList.add(mg);
		}
		return manageGradeList;
	}
}
